// Decompiled on 周六 2月 22 19:31:49 CST 2025 with Zomboid Decompiler v0.1.3 using Vineflower.
package zombie.scripting.objects;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.LinkedList;
import java.util.List;
import java.util.function.Predicate;
import zombie.characters.IsoGameCharacter;
import zombie.inventory.InventoryItem;
import zombie.inventory.types.DrainableComboItem;
import zombie.util.Type;

public final class Fixing extends BaseScriptObject {
    private String name = null;
    private ArrayList<String> require = null;
    private final LinkedList<Fixing.Fixer> fixers = new LinkedList();
    private Fixing.Fixer globalItem = null;
    private float conditionModifier = 1.0F;
    private static final Fixing.PredicateRequired s_PredicateRequired = new Fixing.PredicateRequired();
    private static final ArrayList<InventoryItem> s_InventoryItems = new ArrayList();

    @Override
    public void Load(String string0, String[] string1) {
        this.setName(string0);

        for (int int0 = 0; int0 < string1.length; int0++) {
            if (!string1[int0].trim().isEmpty() && string1[int0].contains(":")) {
                String[] string2 = string1[int0].split(":");
                String string3 = string2[0].trim();
                String string4 = string2[1].trim();
                if (string3.equals("Require")) {
                    List list0 = Arrays.asList(string4.split(";"));

                    for (int int1 = 0; int1 < list0.size(); int1++) {
                        this.addRequiredItem(((String)list0.get(int1)).trim());
                    }
                } else if (!string3.equals("Fixer")) {
                    if (string3.equals("GlobalItem")) {
                        if (string4.contains("=")) {
                            this.setGlobalItem(new Fixing.Fixer(string4.split("=")[0], null, Integer.parseInt(string4.split("=")[1])));
                        } else {
                            this.setGlobalItem(new Fixing.Fixer(string4, null, 1));
                        }
                    } else if (string3.equals("ConditionModifier")) {
                        this.setConditionModifier(Float.parseFloat(string4.trim()));
                    }
                } else if (!string4.contains(";")) {
                    if (string4.contains("=")) {
                        this.fixers.add(new Fixing.Fixer(string4.split("=")[0], null, Integer.parseInt(string4.split("=")[1])));
                    } else {
                        this.fixers.add(new Fixing.Fixer(string4, null, 1));
                    }
                } else {
                    LinkedList linkedList = new LinkedList();
                    List list1 = Arrays.asList(string4.split(";"));

                    for (int int2 = 1; int2 < list1.size(); int2++) {
                        String[] string5 = ((String)list1.get(int2)).trim().split("=");
                        linkedList.add(new Fixing.FixerSkill(string5[0].trim(), Integer.parseInt(string5[1].trim())));
                    }

                    if (string4.split(";")[0].trim().contains("=")) {
                        String[] string6 = string4.split(";")[0].trim().split("=");
                        this.fixers.add(new Fixing.Fixer(string6[0], linkedList, Integer.parseInt(string6[1])));
                    } else {
                        this.fixers.add(new Fixing.Fixer(string4.split(";")[0].trim(), linkedList, 1));
                    }
                }
            }
        }
    }

    public String getName() {
        return this.name;
    }

    public void setName(String _name) {
        this.name = _name;
    }

    public ArrayList<String> getRequiredItem() {
        return this.require;
    }

    public void addRequiredItem(String _require) {
        if (this.require == null) {
            this.require = new ArrayList();
        }

        this.require.add(_require);
    }

    public LinkedList<Fixing.Fixer> getFixers() {
        return this.fixers;
    }

    public Fixing.Fixer usedInFixer(InventoryItem itemType, IsoGameCharacter chr) {
        for (int _int = 0; _int < this.getFixers().size(); _int++) {
            Fixing.Fixer fixer = (Fixing.Fixer)this.getFixers().get(_int);
            if (fixer.getFixerName().equals(itemType.getType())) {
                if (itemType instanceof DrainableComboItem drainableComboItem) {
                    if (!(drainableComboItem.getUsedDelta() < 1.0F)) {
                        return fixer;
                    }

                    if (drainableComboItem.getDrainableUsesInt() >= fixer.getNumberOfUse()) {
                        return fixer;
                    }
                } else if (chr.getInventory().getCountTypeRecurse(this.getModule().getName() + "." + fixer.getFixerName()) >= fixer.getNumberOfUse()) {
                    return fixer;
                }
            }
        }

        return null;
    }

    public InventoryItem haveGlobalItem(IsoGameCharacter chr) {
        s_InventoryItems.clear();
        ArrayList arrayList = this.getRequiredFixerItems(chr, this.getGlobalItem(), null, s_InventoryItems);
        return arrayList == null ? null : (InventoryItem)arrayList.get(0);
    }

    public InventoryItem haveThisFixer(IsoGameCharacter chr, Fixing.Fixer fixer, InventoryItem brokenObject) {
        s_InventoryItems.clear();
        ArrayList arrayList = this.getRequiredFixerItems(chr, fixer, brokenObject, s_InventoryItems);
        return arrayList == null ? null : (InventoryItem)arrayList.get(0);
    }

    public int countUses(IsoGameCharacter chr, Fixing.Fixer fixer, InventoryItem brokenObject) {
        s_InventoryItems.clear();
        s_PredicateRequired.uses = 0;
        this.getRequiredFixerItems(chr, fixer, brokenObject, s_InventoryItems);
        return s_PredicateRequired.uses;
    }

    private static int countUses(InventoryItem inventoryItem) {
        DrainableComboItem drainableComboItem = Type.tryCastTo(inventoryItem, DrainableComboItem.class);
        return drainableComboItem != null ? drainableComboItem.getDrainableUsesInt() : 1;
    }

    public ArrayList<InventoryItem> getRequiredFixerItems(IsoGameCharacter chr, Fixing.Fixer fixer, InventoryItem brokenItem, ArrayList<InventoryItem> items) {
        if (fixer == null) {
            return null;
        } else {
            assert Thread.currentThread() == zombie.GameWindow.GameThread;

            Fixing.PredicateRequired predicateRequired = s_PredicateRequired;
            predicateRequired.fixer = fixer;
            predicateRequired.brokenItem = brokenItem;
            predicateRequired.uses = 0;
            chr.getInventory().getAllRecurse(predicateRequired, items);
            return predicateRequired.uses >= fixer.getNumberOfUse() ? items : null;
        }
    }

    public ArrayList<InventoryItem> getRequiredItems(IsoGameCharacter chr, Fixing.Fixer fixer, InventoryItem brokenItem) {
        ArrayList arrayList = new ArrayList();
        if (this.getRequiredFixerItems(chr, fixer, brokenItem, arrayList) == null) {
            arrayList.clear();
            return null;
        } else if (this.getGlobalItem() != null && this.getRequiredFixerItems(chr, this.getGlobalItem(), brokenItem, arrayList) == null) {
            arrayList.clear();
            return null;
        } else {
            return arrayList;
        }
    }

    public Fixing.Fixer getGlobalItem() {
        return this.globalItem;
    }

    public void setGlobalItem(Fixing.Fixer _globalItem) {
        this.globalItem = _globalItem;
    }

    public float getConditionModifier() {
        return this.conditionModifier;
    }

    public void setConditionModifier(float _conditionModifier) {
        this.conditionModifier = _conditionModifier;
    }

    public static final class Fixer {
        private String fixerName = null;
        private LinkedList<Fixing.FixerSkill> skills = null;
        private int numberOfUse = 1;

        public Fixer(String name, LinkedList<Fixing.FixerSkill> _skills, int _numberOfUse) {
            this.fixerName = name;
            this.skills = _skills;
            this.numberOfUse = _numberOfUse;
        }

        public String getFixerName() {
            return this.fixerName;
        }

        public LinkedList<Fixing.FixerSkill> getFixerSkills() {
            return this.skills;
        }

        public int getNumberOfUse() {
            return this.numberOfUse;
        }
    }

    public static final class FixerSkill {
        private String skillName = null;
        private int skillLvl = 0;

        public FixerSkill(String _skillName, int _skillLvl) {
            this.skillName = _skillName;
            this.skillLvl = _skillLvl;
        }

        public String getSkillName() {
            return this.skillName;
        }

        public int getSkillLevel() {
            return this.skillLvl;
        }
    }

    private static final class PredicateRequired implements Predicate<InventoryItem> {
        Fixing.Fixer fixer;
        InventoryItem brokenItem;
        int uses;

        public boolean test(InventoryItem inventoryItem) {
            if (this.uses >= this.fixer.getNumberOfUse()) {
                return false;
            } else if (inventoryItem == this.brokenItem) {
                return false;
            } else if (!this.fixer.getFixerName().equals(inventoryItem.getType())) {
                return false;
            } else {
                int _int = Fixing.countUses(inventoryItem);
                if (_int > 0) {
                    this.uses += _int;
                    return true;
                } else {
                    return false;
                }
            }
        }
    }
}
